local Msg = require("core.Msg")
local LvConfig = require("excel.Fuli").lv
local GrowConfig = require("excel.Fuli").grow
local FuliDefine = require("fuli.FuliDefine")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local Handler = require("fuli.HandlerL")
local ObjHuman = require("core.ObjHuman")
local ShopHelp = require("shop.ShopHelp")
local Log = require("common.Log")
local HtmlUtil = require("common.HtmlUtil")

--等级特惠奖励列表
function getLvRewardList(human)
  local sendMsg = Msg.gc.GC_FULI_REWARD
  local cache = {}
  sendMsg.list[0] = #LvConfig
  for i=1, #LvConfig do
    sendMsg.list[i].page = FuliDefine.FULI_TYPE_LV
    sendMsg.list[i].canGet = human.db.fuliIsGet[FuliDefine.FULI_TYPE_LV * 15 + i - 1]
    sendMsg.list[i].vipCanGet = 0
    sendMsg.list[i].condition = LvConfig[i].lv
    sendMsg.list[i].needYuanbao = LvConfig[i].needYuanbao
    sendMsg.list[i].reward[0] = #LvConfig[i].reward
    for j=1, #LvConfig[i].reward do
      Grid.create(cache, LvConfig[i].reward[j][1], LvConfig[i].reward[j][2])
      Grid.makeItem(cache, sendMsg.list[i].reward[j], 0, human)
    end    
    sendMsg.list[i].vipReward[0] = 0 
    sendMsg.list[i].onlineTime = 0
    sendMsg.list[i].percent = 0
  end
  Msg.send(sendMsg, human.fd)
  getGrowRewardList(human)
end

--成长特惠奖励列表
function getGrowRewardList(human)
  local sendMsg = Msg.gc.GC_FULI_REWARD
  local cache = {}
  sendMsg.list[0] = #GrowConfig
  for i=1, #GrowConfig do
    sendMsg.list[i].page = FuliDefine.FULI_TYPE_GROW
    sendMsg.list[i].canGet = human.db.fuliIsGet[FuliDefine.FULI_TYPE_GROW * 15 + i - 1]
    sendMsg.list[i].vipCanGet = GrowConfig[i].showType
    sendMsg.list[i].condition = GrowConfig[i].lv
    sendMsg.list[i].needYuanbao = GrowConfig[i].newPrice
    sendMsg.list[i].reward[0] = #GrowConfig[i].reward
    for j=1, #GrowConfig[i].reward do
      Grid.create(cache, GrowConfig[i].reward[j][1], GrowConfig[i].reward[j][2])
      Grid.makeItem(cache, sendMsg.list[i].reward[j], 0, human)
    end    
    sendMsg.list[i].vipReward[0] = 0 
    sendMsg.list[i].onlineTime = GrowConfig[i].oldPrice
    sendMsg.list[i].percent = GrowConfig[i].limitTimes - human.db.fuliGrowBuyTimes[i - 1]
  end
  Msg.send(sendMsg, human.fd)
end

function getLvIndex(human, condition)
  for i=1, #LvConfig do
    if LvConfig[i].lv == condition then
      return i
    end
  end
  return 0
end
function getGrowIndex(human, condition)
  for i=1, #GrowConfig do
    if GrowConfig[i].lv == condition then
      return i
    end
  end
  return 0
end

function getLvReward(human, condition)
  local index = getLvIndex(human, condition)  
  if index == 0 then
    return
  end
  
  if human.db.lv < condition then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.LV_NOT_ENOUGH)
  end
  
  if human.db.fuliIsGet[FuliDefine.FULI_TYPE_LV * 15 + index - 1] == FuliDefine.REWARD_CAN_BUY_SECOND then
    if not ObjHuman.checkYuanbaoEnough(human, LvConfig[index].needYuanbao) then
      return ShopHelp.onNoRMB(human)
    end
  elseif human.db.fuliIsGet[FuliDefine.FULI_TYPE_LV * 15 + index - 1] == FuliDefine.REWARD_NOT_CAN_GET or
    human.db.fuliIsGet[FuliDefine.FULI_TYPE_LV * 15 + index - 1] == FuliDefine.REWARD_ALREADY_GET then
    return
  end
  if not BagLogic.checkCanAddItemList(human, LvConfig[index].reward,#LvConfig[index].reward) then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.FULI_BAG_NOT_ENOUGH)
  end
  
  local type = 1
  if human.db.fuliIsGet[FuliDefine.FULI_TYPE_LV * 15 + index - 1] == FuliDefine.REWARD_CAN_GET then
    human.db.fuliIsGet[FuliDefine.FULI_TYPE_LV * 15 + index - 1] = FuliDefine.REWARD_CAN_BUY_SECOND
    type = 1
    if LvConfig[index].lv > 30 then
      local str = string.format(Lang.FULI_LV_GET_BRO, HtmlUtil.fontName(human.name, human._id), LvConfig[index].lv, human._id)
      Broadcast.sendBroadcast2World(Lang.UP + Lang.GROSSIP, str)
    end
  elseif human.db.fuliIsGet[FuliDefine.FULI_TYPE_LV * 15 + index - 1] == FuliDefine.REWARD_CAN_BUY_SECOND then
    ObjHuman.decYuanbao(human, LvConfig[index].needYuanbao, "fulidating")    
    human.db.fuliIsGet[FuliDefine.FULI_TYPE_LV * 15 + index - 1] = FuliDefine.REWARD_ALREADY_GET
    type = 2
    if LvConfig[index].lv > 30 then
      local str = string.format(Lang.FULI_LV_BUY_BRO, HtmlUtil.fontName(human.name, human._id), LvConfig[index].lv, human._id)
      Broadcast.sendBroadcast2World(Lang.UP + Lang.GROSSIP, str)
    end
  end
  for i=1, #LvConfig[index].reward do
    BagLogic.addItem(human, LvConfig[index].reward[i][1], 1, LvConfig[index].reward[i][2], "fulidating")
  end  
  getLvRewardList(human)
  Handler.checkShowTips(human)  
  Log.write(Log.LOGID_OSS_LV_TEHUI, human._id, human.account, human.name,human.db.lv,condition,type, (type == 1) and 0 or LvConfig[index].needYuanbao)
end

function getGrowReward(human, condition)
  local index = condition
--  if human.db.fuliIsGet[FuliDefine.FULI_TYPE_GROW * 15 + index - 1] ~= FuliDefine.REWARD_CAN_GET then
--    return
--  end
  if human.db.fuliGrowBuyTimes[index - 1] >= GrowConfig[index].limitTimes then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.FULI_GROW_NOT_CAN_BUY)      
  end
  if not BagLogic.checkCanAddItemList(human, GrowConfig[index].reward,#GrowConfig[index].reward) then
     return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.FULI_BAG_NOT_ENOUGH)     
  end
  if not ObjHuman.checkYuanbaoEnough(human, GrowConfig[index].newPrice) then
     return ShopHelp.onNoRMB(human)
  end
  ObjHuman.decYuanbao(human, GrowConfig[index].newPrice, "fulidating")
  for i=1, #GrowConfig[index].reward do
    BagLogic.addItem(human, GrowConfig[index].reward[i][1], 1, GrowConfig[index].reward[i][2], "fulidating")
  end
  human.db.fuliGrowBuyTimes[index - 1] = human.db.fuliGrowBuyTimes[index -1] + 1
  getGrowRewardList(human)
  Broadcast.sendBroadcast(human, Lang.DOWN, Lang.FULI_GROW_BUY_SUCCESS)
  local grid = {}
  grid = Grid.create(grid, GrowConfig[index].reward[1][1], 1, true)
  local str = string.format(Lang.FULI_GROE_BUY_BRO, HtmlUtil.fontName(human.name, human._id), HtmlUtil.fontItem(grid), human._id)
  Broadcast.sendBroadcast2World(Lang.UP + Lang.GROSSIP, str)
  Log.write(Log.LOGID_OSS_GROW_TEHUI, human._id, human.account, human.name,human.db.lv,GrowConfig[index].lv,GrowConfig[index].reward[1][1], GrowConfig[index].newPrice)
end


function checkShowTips(human)
  for i=1, #LvConfig do
    if human.db.fuliIsGet[FuliDefine.FULI_TYPE_LV * 15 + i - 1] == FuliDefine.REWARD_CAN_GET then
      return 1
    end
  end
  return 0
end

function checkCanGetReward(human)
  for i=1, #LvConfig do
    if human.db.lv >= LvConfig[i].lv and human.db.fuliIsGet[FuliDefine.FULI_TYPE_LV * 15 + i - 1] == FuliDefine.REWARD_NOT_CAN_GET then
      human.db.fuliIsGet[FuliDefine.FULI_TYPE_LV * 15 + i - 1] = FuliDefine.REWARD_CAN_GET
    end
  end
  getLvRewardList(human)
end